home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
challenge
/
12.08-Aug96
/
MazeTestCode.sit
/
MazeTestCode ƒ
/
MazeTestMain.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-21
|
4KB
|
130 lines
#include "Maze.h"
#include <stdio.h>
#include <stddef.h>
#include "timer.h"
#define DBG 0
static enum {rock=0,empty=1} CellType;
/* Insert maze files you wish to test here */
/* See ReadMaze for file format if you wish to create your own */
static char *testFiles[] = {"maze1.dat",
"maze2.dat",
"maze3.dat"};
#define kNumFiles 3
/* Some defines for the callback to read the maze data structure */
#define MazeVal(maze,x,y,z) *(maze + (x) + (y)*gxSize + (z)*gxSize*gySize)
#define OutOfBounds(x,y,z) \
((x<0) || (y<0) || (z<0) || (x>=gxSize) || (y>=gySize) || (z>=gzSize) )
#define OnBoundary(x,y,z) \
((x==0) || (y==0) || (z==0) || (x==gxSize-1) || (y==gySize-1) || (z==gzSize-1) )
static char * ReadMaze(FILE *fileName,
long *xSize,long *ySize,long *zSize,long *xStart,long *yStart,long *zStart)
{
size_t numread;
char *maze;
numread = fread(xSize,sizeof(long),1,fileName);
numread = fread(ySize,sizeof(long),1,fileName);
numread = fread(zSize,sizeof(long),1,fileName);
numread = fread(xStart,sizeof(long),1,fileName);
numread = fread(yStart,sizeof(long),1,fileName);
numread = fread(zStart,sizeof(long),1,fileName);
maze = NewPtr(*xSize**ySize**zSize);
numread = fread(maze,1,*xSize**ySize**zSize,fileName);
return maze;
}
/*
* Private storage used by callback to check validity of moves
* gMaze stores the maze, with MazeVal(x,y,z)==empty for empty cells
* gxSize,gySize,gzSize are the maze dimensions
* gxPos,gyPos,gzPos is current position
*/
static long gxPos,gyPos,gzPos;
static long gxSize,gySize,gzSize;
static char *gMaze;
/* Callback */
static Boolean /* found exit */ MakeAMove(
long xMove, /* amount of attempted move in x direction(1,0,-1) */
long yMove, /* amount of attempted move in y direction(1,0,-1) */
long zMove, /* amount of attempted move in z direction (1,0, -1) */
long *newXPos, /* new x position after attempted move */
long *newYPos, /* new y position after attempted move */
long *newZPos /* new y position after attempted move */
){
static long x,y,z;
Boolean result;
*newXPos = gxPos; *newYPos = gyPos; *newZPos = gzPos;
if ( (xMove>1) || (xMove<-1) || (yMove>1) || (yMove<-1) || (zMove>1) || (zMove<-1)) {
#if DBG
DebugStr("\p illegal move size");
#endif
return false; /* illegal move */
}
x = gxPos+xMove; y = gyPos+yMove; z = gzPos+zMove;
if (OutOfBounds(x,y,z)) {
#if DBG
printf("illegal move attempt %ld %ld %ld\n",x,y,z);
#endif
return false; /* illegal move */
}
if (empty != MazeVal(gMaze,x,y,z)) {
return false; /* attempt to move into rock */
}
while ( (z>0) && (empty==MazeVal(gMaze,x,y,z-1)) ) --z; /* fall */
gxPos = *newXPos = x; gyPos = *newYPos = y; gzPos = *newZPos = z;
result = OnBoundary(x,y,z);
return result;
}
static void SubWide(UnsignedWide *a, UnsignedWide *b, UnsignedWide *diff)
{
*diff = *b;
diff->hi -= a->hi;
diff->lo -= a->lo;
if (diff->lo > b->lo) --diff->hi;
}
int main()
{
FILE *outFile;
char *mapStorage;
long xStart,yStart,zStart;
UnsignedWide startTime,endTime,diffTime;
int theFile;
Boolean solved;
InitGraf(&qd.thePort);
for (theFile=0; theFile<kNumFiles; ++theFile) {
outFile = fopen(testFiles[theFile],"rb");
gMaze = ReadMaze(outFile,&gxSize,&gySize,&gzSize,&xStart,&yStart,&zStart);
mapStorage = NewPtrClear(gxSize*gySize*gzSize);
if (nil==mapStorage) DebugStr("\p storage allocation error");
gxPos = xStart; gyPos = yStart; gzPos = zStart;
Microseconds(&startTime);
solved = Maze(xStart,yStart,zStart,gxSize,gySize,gzSize,MakeAMove,mapStorage);
Microseconds(&endTime);
SubWide(&startTime,&endTime,&diffTime);
if (!solved) printf("Entry returned without claiming to solve maze.\n");
else if (!OnBoundary(gxPos,gyPos,gzPos))
printf("Entry did not solve maze - %ld %ld %ld not on boundary.\n",gxPos,gyPos,gzPos);
else printf("Maze exited at x=%ld y=%ld z=%ld\n",gxPos,gyPos,gzPos);
printf("Elapsed time %lu %lu\n",diffTime.hi,diffTime.lo);
fclose(outFile);
DisposePtr(mapStorage);
}
return 0;
}